﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.Aparts.Web.Handlers;
using blackhouse.Aparts.Web.Model;
using blackhouse.Aparts.Web.Modules;
using blackhouse.Aparts.Web.Presenter;
using blackhouse.Aparts.Web.View;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.Opinions;
using blackhouse.BlackApart.Web;
using blackhouse.JavaScript.JQuery;
using blackhouse.mojoPortalExtend.Breadcrumbs;
using blackhouse.Web;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using blackhouse.mojoPortalExtend;

namespace blackhouse.StareKino.Web {
    public partial class Opinions : OwnedNonCmsBasePage, IOpinionsView {

        #region Fields

        private OpinionsPresenter presenter;
        private int apartmentTypeId = -1;

        #endregion

        #region Properties

        public double AverageReviews { get; set; }
        public int OpinionsCount { get; set; }
        public int SatisfactedClients { get; set; }

        public int ApartmentTypeId {
            get {
                if (this.apartmentTypeId == -1)
                    this.apartmentTypeId = this.GetApartmentTypeId();
                return this.apartmentTypeId;
            }
        }

        public IDictionary<string, double> ReviewCategoriesValue {
            get {
                return this.ctrlOpinionCategories.Categories;
            }
            set {
                this.ctrlOpinionCategories.Categories = value;
            }
        }

        public IList<IOpinion> DisplayOpinions {
            get {
                return this.rptOpinions.DataSource as IList<IOpinion>;
            }
            set {
                this.rptOpinions.DataSource = value;
            }
        }

        private bool haveOpinionAdminPriv;
        private bool haveOpinionAdminPrivSetted;

        public bool HaveOpinionAdminPriv {
            get {
                if (!this.haveOpinionAdminPrivSetted) {
                    this.haveOpinionAdminPriv = WebUser.IsAdminOrContentAdminOrContentPublisher || WebUser.IsInRole("Opinions");
                    this.haveOpinionAdminPrivSetted = true;
                }
                return this.haveOpinionAdminPriv;
            }
        }

        #endregion

        #region Methods

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            if (this.IsPostBack) {
                int atid = 0;
                string sAT = Request[this.apartmentType.UniqueID];
                if (!string.IsNullOrEmpty(sAT) && Int32.TryParse(sAT, out atid) && atid != this.ApartmentTypeId)
                    Response.Redirect("?apartment=" + atid);

            }

            this.AddClassToBody("short-top");
            this.presenter = new OpinionsPresenter(this, new ApartsOpinionsModel());
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.presenter.LoadSummaryReviews();
            if (this.RequestHtmlSnapshot())
                this.presenter.LoadAllOpinions();
            else
                this.presenter.LoadOpinions();

            this.ctrlOpinionCategories.AddOpinionNumber(this.AverageReviews, "średnia ocena");
            this.ctrlOpinionCategories.AddOpinionNumber(this.OpinionsCount, "opinie", "F0");
            this.ctrlOpinionCategories.AddOpinionNumber(this.SatisfactedClients, "zadowolonych klientów", "F0");

            this.DataBind();
            if (!this.RequestHtmlSnapshot())
                this.SupportAjaxContent();

            this.apartmentType.Value = this.ApartmentTypeId.ToString();

            this.AdditionalMetaMarkup = "<meta name=\"fragment\" content=\"!\">";

            this.SetPageSettingsFromCms();
        }

        private int GetApartmentTypeId() {
            int apartmentTypeId = 0;
            string sApartment = Request.QueryString["apartment"];
            if (!string.IsNullOrEmpty(sApartment) && Int32.TryParse(sApartment, out apartmentTypeId))
                return apartmentTypeId;
            else
                return 0;
        }

        private bool RequestHtmlSnapshot() {
            return Request.QueryString.AllKeys.Contains("_escaped_fragment_");
        }

        public void SupportAjaxContent() {
            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeJQueryTemplates();
            jql.IncludeJQueryWaypoints();

            ApartmentInfo.AdJs();

            jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.opinions.js,blackhouse.Aparts.Web");
            // jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.opinion-apartments.js,blackhouse.Aparts.Web");

            blackhouse.Web.Controls.TemplateControl.TemplateControl tplCtrl = new blackhouse.Web.Controls.TemplateControl.TemplateControl();
            tplCtrl.TemplateId = "opinion-item";
            tplCtrl.Controls.Add(Page.LoadControl("~/Modules/OpinionItem.ascx"));
            this.Page.Header.Controls.Add(tplCtrl);
        }

        public override void DataBind() {
            this.rptOpinions.DataBind();
            base.DataBind();
        }

        public void ShowNoOpinionsInfo() {
            this.opinionsLoad.Visible = false;
            this.noOpinions.Visible = true;
        }

        #endregion

    }
}